package com.data.migration.service;

import com.data.migration.entity.Operation;
import com.data.migration.util.DBUtil;
import com.data.migration.vo.TeacherVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peter on 2018/12/8.
 * 迁移1v1课程数据
 */
public class Step_8_Teacher_Others {
    public static final String step = "8";
    public static final String stepDesc = "老师其他信息";

    private final Logger logger = LoggerFactory.getLogger(Step_8_Teacher_Others.class);
    public List<TeacherVo> teachers = new ArrayList<>();
    public List<Operation> insertSql = new ArrayList<>();//插入课程的sql

    public static void main(String[] args) throws SQLException {
        Step_8_Teacher_Others step = new Step_8_Teacher_Others();
        step.start();
//        step.clean();

    }

    public void start() throws SQLException {
        logger.info("\n******************************************************************************" +
                "\n======================== Step 8: 老师其他信息 start ================================" +
                "\n******************************************************************************\n");
        step_1();
        step_1_2();
        step_1_3();
        logger.info("\n******************************************************************************" +
                "\n======================== Step 8: 老师其他信息 end ================================" +
                "\n******************************************************************************\n");
    }

    public void clean() throws SQLException {
        logger.info("\n******************************************************************************" +
                "\n======================== 【clean】 Step 8: 老师其他信息 start =========================" +
                "\n******************************************************************************");
        step_1_clean();
        logger.info("\n******************************************************************************" +
                "\n======================== 【clean】 Step 8: 老师其他信息 end =========================" +
                "\n******************************************************************************");
    }

    // 1. 老师账单 【td_nt_tea_found_change】     -------------------------------------------------------------------------------2
    public void step_1() throws SQLException {
        logger.info("\n     --------- 8.1. 老师账单 【td_nt_tea_found_change】 start --------- ");
        Connection conn = DBUtil.getConn(Operation.app_type_T);
        String tableName = "td_nt_tea_found_change"; // 表名
        String pk = "TFCHG_ID"; // 主键（自增）的列名字
        insertSql = DBUtil.buildInsertSql(conn, tableName, pk, step, stepDesc);

        DBUtil.insertBatch(insertSql);
    }

    // 1.2. 更新外键【td_nt_tea_found_change.TEA_ID】
    public void step_1_2() throws SQLException {
        String ftableName = "td_nt_tea_found_change"; // 附表表名
        String fk = "TEA_ID";// 附表外键
        String ptableName = "td_nt_teacher";//主表的表名
        String pk = "tea_id"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

    // 1.3. 更新外键【td_nt_tea_found_change.goods_id】
    public void step_1_3() throws SQLException {
        String ftableName = "td_nt_tea_found_change"; // 附表表名
        String fk = "goods_id";// 附表外键
        String ptableName = "td_nt_reserve";//主表的表名
        String pk = "reserve_id"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

    public void step_1_clean() throws SQLException {
        logger.info("\n     ---------【clean】 8.1. 老师账单 【td_nt_tea_found_change】 start --------- ");
        String sql = "delete from td_nt_tea_found_change where app_type='T'";
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

}
